package com.kitty.game.utils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    /**
     * 一毫秒
     */
    public static final long ONE_MILLSECOND = 1L;
    /**
     * 一秒的毫秒数
     */
    public static final long ONE_SECOND = ONE_MILLSECOND * 1000;
    /**
     * 一分的毫秒数
     */
    public static final long ONE_MINUTE = ONE_SECOND * 60;
    /**
     * 一时的毫秒数
     */
    public static final long ONE_HOUR = ONE_MINUTE * 60;
    /**
     * 一天的毫秒数
     */
    public static final long ONE_DAY = ONE_HOUR * 24;
    /**
     * 一天总共有{@value}小时
     */
    public static final int HOURS_OF_DAY = 24;

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static int getDaysOfMonth() {
        Calendar cl = Calendar.getInstance();
        int dayCount = cl.getActualMaximum(Calendar.DATE);
        return dayCount;
    }

    public static String formatDate(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    public static String formatDate(Date date, String formatPattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);
        return simpleDateFormat.format(date);
    }

    public static String getDay(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        return simpleDateFormat.format(date);
    }


    public static Date parse(String datestr) {
        return parse(datestr, "yyyy-MM-dd");
    }

    public static Date parseTime(String datestr) {
        return parse(datestr, "HH:mm:ss");
    }

    public static Date parse(String datestr, String datePattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
        try {
            return simpleDateFormat.parse(datestr);
        } catch (Exception e) {
        }
        return null;
    }
    /**
     * @author：my
     * @describe：当前日期减去 time = time前的日期
     */
    public static String subtraction(Integer time) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_MONTH, -time);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(now.getTime());
    }



    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
}
